<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Building controller
 */
class Cases extends Api_Controller
{
    /**
     * @apiDefine Cases 案例
     */
    function __construct()
    {
        // Construct the parent class
        parent::__construct();
        $this->load->model('article_m');
        $this->load->model('car_m');
    }

    /**
     * @api               {get} cases/index 获取车型案例列表
     * @apiGroup          Cases
     * @apiName           cases/index
     * @apiVersion        1.0.0
     * @apiDescription    车型列表
     * @apiSampleRequest https://api.bjxpel.cn/api/cases/index
     * @apiParam {Int} car_id 车型id
     * @apiParam {Int} [page] 页数,默认1
     * @apiParam {Int} [limit] 页显示数量，默认5
     * @apiSuccess {String} title 标题
     * @apiSuccess {String} img 封面图
     * @apiSuccessExample 成功响应
     *          {
     *           "message": "ok",
     *           "code": 200,
     *           "data": null
     *          }
     * @apiErrorExample   失败响应
     *            {
     *             "message": "err",
     *             "code": 0,
     *             "data": null
     *            }
     */
    public function index()
    {
        $car_id = $this->input->get('car_id') ?? 0;
        $page   = $this->input->get('page') ?? 1;
        $limit  = $this->input->get('limit') ?? 5;
        $start  = ($page - 1) * $limit;
        if (!$car_id) {
            return $this->failure(null, "缺少参数车型id");
        }
        $where = ['car_category' => $car_id, 'deletetd' => 0];
        $count = $this->article_m->total($where);
        $cases = $this->article_m->select($where, $limit, $start, ['id', 'title', 'img']);
        if (!$cases) {
            return $this->failure();
        }
        return $this->success(['total' => $count, 'list' => $cases]);
    }

    /**
     * @api               {get} cases/detail 获取车型案例详情
     * @apiGroup          Cases
     * @apiName           cases/detail
     * @apiVersion        1.0.0
     * @apiDescription    车型列表
     * @apiSampleRequest https://api.bjxpel.cn/api/cases/detail
     * @apiParam {Int} [case_id] 车型案例id
     * @apiSuccess {String} title 标题
     * @apiSuccess {String} img 封面图
     * @apiSuccessExample 成功响应
     *          {
     *           "message": "ok",
     *           "code": 200,
     *           "data": null
     *          }
     * @apiErrorExample   失败响应
     *            {
     *             "message": "err",
     *             "code": 0,
     *             "data": null
     *            }
     */
    public function detail()
    {
        $case_id = $this->input->get('case_id') ?? 0;
        if (!$case_id) {
            return $this->failure(null, "缺少参数车型文章id");
        }
        $where = ['id' => $case_id, 'deletetd' => 0];
        $cases = $this->article_m->findBy($where);
        if (!$cases) {
            return $this->failure(null, "车型案例不存在或已被删除");
        }
        // 案例分类
        $car                        = $this->car_m->find($cases['car_category']);
        $cases['car_category_name'] = $car['car_name'] ?? "";
        return $this->success($cases);
    }

}